package com.cash.loan.modular.cash.service.impl;

import com.cash.loan.core.util.RandomUtil;
import com.cash.loan.modular.cash.dao.LoanRecommendMapper;
import com.cash.loan.modular.cash.service.ProductIndexService;
import com.cash.loan.modular.cash.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @description
 * @date 2018年10月02 18:48
 */
@Service
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private LoanRecommendMapper recommendMapper;

    @Autowired
    private ProductIndexServiceImpl productIndexService;

    @Override
    public Map<String, Object> refresh(Integer id) {
        List<Integer> recommendIdList = recommendMapper.getRecommendIdList(id);
        if(recommendIdList == null || recommendIdList.isEmpty()) {
            //如果为空，返回本身数据
            Map<String,Object> result = recommendMapper.selectRecommendById(id);
            if(result != null) {
                result = productIndexService.dealRecommendResult(result);
            }
            return result;
        }
        Integer index = RandomUtil.nextInt(recommendIdList.size() - 1);
        Integer anotherId = recommendIdList.get(index);
        Map<String,Object> result = recommendMapper.selectRecommendById(anotherId);
        if(result != null) {
            result = productIndexService.dealRecommendResult(result);
        }
        return result;
    }

    @Override
    public Map<String, Object> getRecommendByNum(Integer num) {
        Map<String,Object> result = recommendMapper.getFieldLimitByFieldId(num);
        if(result != null) {
            result = productIndexService.dealRecommendResult(result);
        }
        return result;
    }
}
